package com.data.structure;

import java.util.Arrays;

/**
 *基于数组实现顺序栈
 */
public class Stack<E> {
    private Object[] data = null;
    private int maxSize = 0;//栈容量
    private int top = -1 ;//栈顶指针
    Stack(){
        this(10);
    }
    Stack(int initalSize){
        if(initalSize>=0){
            this.maxSize = initalSize;
            data = new Object[initalSize];
            top = -1;
        }else{
            throw new RuntimeException("初始化大小不能小于0:"+initalSize);
        }
    }
    //第一个元素，top=0
    public boolean push(E e){
        if(top==maxSize-1){
            throw new RuntimeException("栈已满，无法将元素入栈");
        }else{
            data[++top] = e;
            System.out.println(Arrays.asList(data));
            return true;
        }
    }

    //弹出栈顶元素
    public E pop(){
        if(top == -1){
            throw new RuntimeException("栈为空!");
        }else{
            return (E) data[top--];
        }
    }

    //查询栈顶元素但不移除
    public E peek(){
        if(top==-1){
            throw new RuntimeException("栈为空!");
        }else{
            return (E) data[top];
        }
    }


    public static void main(String[] args){
        Stack stack = new Stack();
        for(int i=100;i<120;i++){

            stack.push(i);
        }
    }

}
